package com.xue.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.thymeleaf.context.WebContext;
import org.thymeleaf.spring5.view.ThymeleafViewResolver;

@Controller
public class PageController {
	
	
	@Autowired
    ApplicationContext applicationContext;
	
	@Autowired
    //ThymeleafViewResolver thymeleafViewResolver;
	ThymeleafViewResolver thymeleafViewResolver;
	 //@RequestMapping(value = "/getHtml", produces = "text/html")
	@RequestMapping("/getHtml")
	 @ResponseBody
	 public ModelAndView list(HttpServletRequest request, HttpServletResponse response, Model model) {
		 System.err.println("**********页面返回********************");
//		 SpringWebContext ctx = new SpringWebContext(request, response,
//	                request.getServletContext(), request.getLocale(), model.asMap(), applicationContext);
//	        html = thymeleafViewResolver.getTemplateEngine().process("goods_list", ctx);

		 model.addAttribute("user", "赵志学");
	     model.addAttribute("goodsList", "产品列表"); 
	     
		 return new ModelAndView("test.html");
	 }
	@RequestMapping("/getHtml2")
	@ResponseBody
	 public String getlist(HttpServletRequest request, HttpServletResponse response, Model model) {
		 System.err.println("**********页面返回********************");
		// SpringWebContext ctx = new SpringWebContext(request, response,
	     //           request.getServletContext(), request.getLocale(), model.asMap(), applicationContext);
//	        html = thymeleafViewResolver.getTemplateEngine().process("goods_list", ctx);
		 model.addAttribute("user", "赵志学");
		 model.addAttribute("goodsList", "产品列表"); 

		 WebContext ctx = new WebContext(request, response, 
				    request.getServletContext(), request.getLocale(), model.asMap());
		 
		 
	     
	     String html  = thymeleafViewResolver.getTemplateEngine().process("test", ctx);
		 return html;
	 }
}
